/*  
 * cafe-config.js
 * @Author :      PANRUSEN
 * @CreateDate :  2017年6月8日
 * Copyright (c) 2017, NRIET.　
 * TODO
 * 		组件配置信息，主要用于模块配置
 * 		模块默认目录为：cafejs/script/,此工程中配置为：/static/widgets/cafejs/script；
 */
;
(function(callFactory) {

    /**-----model configuration start-----**/

    //基础URL 相对于webcontext路径
    var basePathJS = '/static/widgets/cafejs/script/';
    var basePathCSS = '/static/widgets/cafejs/themes/default/';
    //模块JS集合
    var modjs = {
        "dateUtil": "utils/dateUtil",
        "typeUtil": "utils/typeUtil",
        "json2": "utils/json2",
        "errorUtil": "utils/errorUtil",
        "encryptUtil": "utils/encryptUtil",
        "windUtil": "utils/windUtil",
        "gridUtil": "utils/gridUtil",
        "css": "cafe-css",
        "router": "cafe-route",
        "layout": "cafe-layout",
        "progress": "cafe-progress",
        "dropdown": "cafe-dropdown",
        "moddialog": "cafe-moddialog",
        "moddialogNew": "cafe-modalNew",
        "select": "cafe-select",
        "nCharts": "nChart/cafe-nCharts",
        "ngrid": "cafe-ngrid",
        "objectivewarning": "cafe-objectivewarning",
        "busmenu": "cafe-busmenu",
        "datepicker": "cafe-datepicker",
        "cafeMap": "cafe-map",
        "warningLegend": 'warningLegend/warningLegend',
        "colorPicker": 'colorPicker/colorPicker',
        "squarelegend": "cafe-squarelegend",
        "assisttools": 'cafe-assisttools',
        "legentconfig": "cafe-legentconfig",
        "legentpicker": "cafe-legentpicker",
        "gistools": "cafe-gistools",
        "drawTool": "gisTool/cafe-gistools",
        "customdatepicker": 'cafe-customdatepicker',
        "dateselect": 'cafe-dateselect',
        "messageRead": "messageRead/messageRead",
        "ajax": 'cafe-ajax',
        "timeLine": "cafe-timeline",
        "timeLine2": "cafe-timeline2",
        "timeLine3": "cafe-timeline3",
        "toolsbar": "cafe-toolsbar",
        "layermanage": "cafe-layermanage",
        "pagination": 'cafe-pagination',
        "vue": "utils/vue.min"
    };
    //模块依赖，*要确保依赖模块已配置,切无论是否唯一都需包装成数值形式
    var jsdeps = {
        'router': ['storage'],
        'datepicker': ['dateUtil', 'dateselect'],
        'customdatepicker': ['dateUtil', 'dateselect'],
        'legentpicker': ['legentconfig']
    };
    //模块CSS集合,value支持string和数组两种形式
    var cssdeps = {
        "moddialogNew": "cafe-modalNew",
        'datepicker': 'cafe-datepicker',
        "squarelegend" : "cafe-squarelegend",
        "legentpicker": "cafe-legentpicker",
        "cafeMap": "cafe-map",
        "gistools": "cafe-gistools",
        "drawTool": "gisTool/cafe-gistools",
        "timeLine": "cafe-timeline",
        "toolsbar": "cafe-toolsbar",
        "timeLine2": "cafe-timeline2",
        "layermanage": "cafe-layermanage"
    };

    /**-----model configuration end-----**/

    var obj = {
        basePathJS: basePathJS,
        basePathCSS: basePathCSS,
        modjs: modjs,
        jsdeps: jsdeps,
        cssdeps: cssdeps
    };
    if (typeof callFactory === 'function') {
        callFactory(obj);
    }

}(function(args) {

    var basePathJS = args["basePathJS"],
        basePathCSS = args["basePathCSS"],
        modjs = args["modjs"],
        jsdeps = args["jsdeps"],
        cssdeps = args["cssdeps"];

    function isArry(obj) {
        return $.type(obj) === 'array' ? true : false;
    }

    function _operlib(jsonData, call) {
        var lbs = jsonData.lib,
            deps = jsonData.deps;
        var obj = {};
        $.each(lbs, function(key, lb) {
            var csss = lb.css;
            var baseheader = lb.basePath,
                reghttp = /^http:*|^https:*/;;
            if (reghttp.test(lb.basePath)) {
                baseheader = lb.basePath;
            }
            require(['css'], function(cssHandler) {
                if (isArry(csss) && csss.length > 0) {
                    $.each(csss, function(index, css) {
                        cssHandler.load(baseheader + css, key + "_css_" + index);
                    });
                }
            });
            if (lb.js && lb.js.length > 0) {
                $.each(lb.js, function(index, jss) {
                    lb.js[index] = baseheader + jss;
                });
                obj[key] = lb.js;
            }
        });
        $.each(deps, function(key, dps) {
            if (dps && dps.length > 0) {
                var arr = [];
                $.each(dps, function(index, arg) {
                    arr = $.merge(arr, obj[arg]);
                });
                setTimeout(function() {
                    obj[key] = {
                        deps: arr
                    };
                }, 20);
            }

        });
        setTimeout(function() {
            if (typeof call === 'function') {
                call(obj);
            }
        }, 60);
    }

    function _getLibs() {
        var jsons = "/static/widgets/cafejs/cafe-lib.json";
        CafeJS.lib = {};
        var lbs = [];
        $.ajax({
            url: jsons,
            type: "get",
            dataType: "json",
            async: false,
            success: function(jsonData) {
                if (jsonData && jsonData.lib) CafeJS.lib = jsonData.lib;
                var libs = jsonData.lib,
                    deps = jsonData.deps;
                _operlib(jsonData, function(obj) {
                    require.config({
                        shim: obj
                    }, null, function(e) {
                        console.error(e);
                    });
                });
            },
            error: function(e) {
                console.log(e);
            }
        });

    }
    //初始化配置主方法
    CafeJS.initConfig = function(callfunc) {
        CafeJS.baseUrl = basePathJS;
        if (!($(modjs).length > 0)) return;
        require.config({
            baseUrl: basePathJS,
            paths: modjs
        }, null, function(e) {
            console.error(e);
        });
        _getLibs();
        try {
            CafeJS.Jsdep = {};
            require(['dateUtil', 'typeUtil', 'json2', 'errorUtil', 'encryptUtil', 'windUtil', 'gridUtil']);
            require(['css'], function(cssHandler) {
                $.each(modjs, function(key, css) {
                    var cssdep = cssdeps[key],
                        jsdep = jsdeps[key];
                    if (isArry(cssdep)) {
                        $.each(cssdep, function(index, css) {
                            cssHandler.load(basePathCSS + css + '.css', key + "_" + index);
                        });
                    } else if (cssdep) {
                        cssHandler.load(basePathCSS + cssdep + '.css', key);
                    }
                    if (jsdep) {
                        CafeJS.Jsdep[key] = jsdep;
                    }
                });
            });
            if (typeof callfunc === 'function') {
                window.setTimeout(callfunc, 100);
            }
        } catch (e) {
            console.error(e);
        }
    };
}));